<?php
/**
 * Author: liqinglin
 * CreateTime: 2018-12-26 16:07
 * description:
 */

namespace app\admin\model;

use think\Db;
use think\helper\Time;
use think\Model;


class Dynamic extends Model
{
    protected $table = 'cn_service_dynamic';
    protected $auto = ['create_time'];

    /**
     * 保存服务商动态
     * @param $data
     * @return Dynamic|int|string
     */
    public static function saveDynamic($data)
    {
        if (empty($data['dynamic_id'])) {
            $data['create_time'] = time();
            $data['update_time'] = time();
            $data['dynamic_id'] = getUniqueId('d_');
            // $service_id = getUniqueId('d_');
            return self::insert($data);
        }

        if ($data['dynamic_id'] && $data['status'] == 0) {
            DynamicErrorMessage::where('dynamic_id', $data['dynamic_id'])->update(['is_del' => 1]);
        }
        $data['update_time'] = time();
        return self::where('dynamic_id', $data['dynamic_id'])->update($data);
    }

    /**
     * 更新动态信息
     * @param $dynamic_id 动态id
     * @param $data
     * @return Dynamic
     */
    public static function updateDynamicStatus($dynamic_id, $data)
    {
        $data['update_time'] = time();
        return self::where('dynamic_id', $dynamic_id)->update($data);
    }

    /**
     * 获取动态列表
     * @param $service_id 服务商id
     */
    public static function getDynamiclist($service_id)
    {
        return self::where('service_id', $service_id)
            ->where('status', 2)
            ->field('dynamic_id,content,show_image,create_time,author,title')
            ->select();
    }

    /**
     * 获取动态列表
     * @param $service_id 服务商id
     */
    public static function getArticlelist($service_id, $is_lately, $page)
    {
        $length = $is_lately ? 2 : 20;
        return Db::name('cn_service_dynamic')->where('service_id', $service_id)
            ->where('status', 2)
            ->limit($page * 10, $length)
            ->field('dynamic_id,show_image,create_time,author,title')
            ->order('create_time', 'desc')
            ->select();

    }

    public function getDynamicInfo($dynamic_id)
    {
        return self::where('dynamic_id', $dynamic_id)->find();
    }

    /**
     * 增加状态查看数量
     * @param $ids 被查看的次数
     * @return false|int
     * @throws \think\exception\DbException
     */
    public static function addLookNumber($ids)
    {

        return self::where('dynamic_id', $ids)->setInc('look_number', 1);
    }

    /**
     * 增加评论数量
     * @param $dynamic_id
     * @return false|int
     * @throws \think\exception\DbException
     */
    public static function addCommentNumber($dynamic_id)
    {
        return self::where('dynamic_id', $dynamic_id)->setInc('comment_number', 1);
    }


}